﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Jugg.Core.MediaItem
{
    public static class StringExtension
    {
        /// <summary>
        /// 去除 任何空白字符
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string trimSpace(this string source)
        {
            return Regex.Replace(source, @"\s", "");
        }

        /// <summary>
        /// 验证是否是手机号码
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool isPhone(this string source)
        { 
            return match(@"^1[345678]\d{9}$", source);
        }

        /// <summary>
        /// 判断是否是大于0正整数
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool isInteger(this string source)
        { 
            return match(@"^[1-9]\d*$", source);
        }

        /// <summary>
        /// 是否是百分比，最多保留2位小数
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool isPercent(this string source)
        {
            return match(@"^(100|[1-9]?\d(\.\d\d?)?)$", source);
        }


        /// <summary>
        /// 判断的正则
        /// </summary>
        /// <param name="parttern"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        private static bool match(string parttern,string input)
        {
            Regex reg = new Regex(parttern);
            return reg.IsMatch(input);
        }

    }
}
